package cn.oracle.eshop.comment.service.impl;

import cn.oracle.eshop.comment.constant.*;

import cn.oracle.eshop.comment.dao.CommentInfoDAO;
import cn.oracle.eshop.comment.domain.CommentInfoDO;
import cn.oracle.eshop.comment.domain.CommentInfoDTO;
import cn.oracle.eshop.comment.service.CommentInfoService;
import cn.oracle.eshop.order.domain.OrderInfoDTO;
import cn.oracle.eshop.order.domain.OrderItemDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 评论信息管理模块的service组件
 *
 * @author wyh
 * @ClassName CommentInfoServiceImpl
 * @date 2022/1/24 /23:37
 */
@Service
@Slf4j
public class CommentInfoServiceImpl implements CommentInfoService {

    @Autowired
    private CommentInfoDAO commentInfoDAO;

    /***
     *新增手动发表的评论信息
     * @author wyh
     * @date 2022/1/24
     * @param commentInfodto 评论信息dTO对象
     * @return
     */
    @Override
    public Boolean saveManualPublishedCommentInfo(CommentInfoDTO commentInfodto) {
        try {
            //计算评论的总分数
            Integer totalScore = Math.round((commentInfodto.getGoodsScore()
                    + commentInfodto.getCustomerServiceScore()
                    + commentInfodto.getLogisticsScore()) / 3);
            commentInfodto.setTotalScore(totalScore);
            //设置是否为默认评论
            commentInfodto.setDefaultComment(DefaultComment.NO);
            //设置评论状态
            commentInfodto.setCommentStatus(CommentStatus.APPROVING);
            //设置评论类型
            Integer commentType = 0;
            if (totalScore >= 4) {
                commentType = CommentType.GOOD_COMMENT;
            } else if (totalScore == 3) {
                commentType = CommentType.MEDIUM_COMMENT;
            } else if (totalScore > 0 && totalScore <= 2) {
                commentType = CommentType.BAD_COMMENT;
            }
            //设置评论类型
            commentInfodto.setCommentType(commentType);
            //设置创建时间和修改时间
            commentInfodto.setGmtCreate(new Date());
            commentInfodto.setGmtModified(new Date());
            //将评论信息保存到数据库中去
            CommentInfoDO commentInfoDO = commentInfodto.clone(CommentInfoDO.class);
            commentInfoDAO.saveCommentInfo(commentInfoDO);
            //设置评论信息的id
            commentInfodto.setId(commentInfoDO.getId());
        } catch (Exception e) {
            log.error("error:{}", e);
            return false;
        }
        return true;

    }
    /***
     * 新增自动发表的评论中心
     * @author wyh
     * @date 2022/2/9
     * @param orderInfoDTO
     * @param orderItemDTO
     * @return cn.oracle.eshop.comment.domain.CommentInfoDTO
    */

    @Override
    public CommentInfoDTO saveAutoPublishedCommentInfo(OrderInfoDTO orderInfoDTO, OrderItemDTO orderItemDTO){
        CommentInfoDTO commentInfoDTO = null;
        try {
            //创建评论信息DTO对象
            commentInfoDTO=createCommentInfoDTO(orderInfoDTO,orderItemDTO);

            //将评论信息保存到数据库中去
            CommentInfoDO commentInfoDO=commentInfoDTO.clone(CommentInfoDO.class);

            commentInfoDAO.saveCommentInfo(commentInfoDO);
            //设置评论信息的ID
            commentInfoDTO.setId(commentInfoDO.getId());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("error:{}",e.getMessage());
            return null;
        }
        return  commentInfoDTO;
    }
/***
 * 创建评论信息DTO对象
 * @author wyh
 * @date 2022/2/9
 * @param orderInfoDTO 订单信息DTO对象
 * @param orderItemDTO 订单条目DTO对象
 * @return 评论信息DTO对象
*/

    private CommentInfoDTO createCommentInfoDTO(OrderInfoDTO orderInfoDTO, OrderItemDTO orderItemDTO) {
        CommentInfoDTO  commentInfoDTO=new CommentInfoDTO();
        commentInfoDTO.setUserAccountId(orderInfoDTO.getUserAccountId());
        commentInfoDTO.setUsername(orderInfoDTO.getUsername());
        commentInfoDTO.setOrderInfoId(orderInfoDTO.getId());
        commentInfoDTO.setOrderItemId(orderItemDTO.getId());
        commentInfoDTO.setGoodsId(orderItemDTO.getGoodsId());
        commentInfoDTO.setGoodsSkuId(orderItemDTO.getGoodsSkuId());
        commentInfoDTO.setGoodsSkuSaleProperties(orderItemDTO.getSaleProperties());
        commentInfoDTO.setTotalScore(CommentInfoScore.FIVE);
        commentInfoDTO.setGoodsScore(CommentInfoScore.FIVE);
        commentInfoDTO.setCustomerServiceScore(CommentInfoScore.FIVE);
        commentInfoDTO.setLogisticsScore(CommentInfoScore.FIVE);
        commentInfoDTO.setCommentContent(CommentContent.DEFAULT);
        commentInfoDTO.setShowPictures(ShowPictures.NO);
        commentInfoDTO.setDefaultComment(DefaultComment.YES);
        commentInfoDTO.setCommentStatus(CommentStatus.APPROVED);
        commentInfoDTO.setCommentType(CommentType.GOOD_COMMENT);
        commentInfoDTO.setGmtCreate(new Date());
        commentInfoDTO.setGmtModified(new Date());

   return  commentInfoDTO;
    }

}
